package com.kgc.ljp.server.util;

import javax.swing.*;
import java.util.Random;
import java.util.Scanner;

/**
 * 验证码生成类
 */
public class SavaCode{
    public static String code(){
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            int a = random.nextInt(3)+1;
            int num = random.nextInt(10);
            switch (a){
                case 1:
                    sb.append(num+"");
                    continue;
                case 2:
                    int chars = (int) (Math.random()*26)+65;
                    sb.append((char)chars+"");
                    continue;
                case 3:
                    int chars2 = (int) (Math.random()*26)+97;
                    sb.append((char)chars2+"");
                    continue;
            }
        }
        return sb.toString();
    }
    public static void useCode(){
        String sCode = null;
        Scanner scan = new Scanner(System.in);
        do {
            System.out.println("请写出右边的验证码:"+ (sCode =SavaCode.code()));
            System.out.print("请输入:");
            String code = scan.next();
            if (sCode.equals(code)){
                System.out.println("验证成功");
                break;
            }else{
                System.out.println("验证码输入不正确!!!");
            }
        }while (true);
    }
}
